package com.example.rouroudate.ui.gallery.ui

import android.content.Context
import android.content.res.ColorStateList
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.example.rouroudate.R
import com.example.rouroudate.ui.widget.CommonBottomDialog
import com.example.rouroudate.ui.widget.DataEventBus
import com.example.rouroudate.ui.widget.ViewPagerTransformer
import com.example.rouroudate.util.CompatUtils
import com.example.rouroudate.util.PrefManager
import com.example.rouroudate.util.ToastUtils
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class StyleSettingDialog constructor(context: Context) : CommonBottomDialog(context) {

    companion object {
        const val TAG = "StyleSettingDialog"
    }

    private lateinit var viewPager: ViewPager

    private var currentPosition = PrefManager.getStyleType() - 1

    private val views = mutableListOf<StyleAnimationView>().apply {
        for (i in StyleAnimationView.typeArray) {
            add(StyleAnimationView(context, i))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.style_animation_view_layout)
        setRightViewClick {
            ToastUtils.show(
                if (PrefManager.setStyleType(StyleAnimationView.typeArray[currentPosition])) {
                    GlobalScope.launch {
                        DataEventBus.styleSettingType.emit(StyleAnimationView.typeArray[currentPosition])
                    }
                    context.getString(R.string.edit_success)
                } else {
                    context.getString(R.string.edit_fail)
                }
            )
            dismiss()
        }
        viewPager = findViewById<ViewPager>(R.id.view_pager)!!.apply {

            addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                }

                override fun onPageSelected(position: Int) {
                    Log.d(TAG, "onPageSelected")
                    currentPosition = position
                    for ((i, v) in views.withIndex()) {
                        if (i == position) {
                            v.startAnimation()
                        } else {
                            v.cancelAnimation()
                        }
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {
                }
            })

            adapter = object : PagerAdapter() {

                override fun getCount() = views.size

                override fun isViewFromObject(view: View, `object`: Any): Boolean {
                    return view == `object`
                }

                override fun instantiateItem(container: ViewGroup, position: Int): Any {
                    Log.d(TAG, "position = $position")
                    val view = views[position]
                    if (view.parent == null) {
                        container.addView(view)
                    }
                    return view
                }

                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                }
            }

            setPageTransformer(true, ViewPagerTransformer())

            currentItem = currentPosition

            if (currentPosition == StyleAnimationView.CircleType - 1) {
                views[currentPosition].startAnimation()
            }
        }
        findViewById<TabLayout>(R.id.tab_layout)!!.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                tooltipText = ""
            }
            setupWithViewPager(viewPager)
            repeat(tabCount) { idx ->
                val tab = getTabAt(idx)!!
                tab.icon = CompatUtils.getTabIcon(idx + 1)
            }
        }
    }
}